package com.wind.ipagent.controller;

import com.wind.ipagent.service.KdlService;
import com.wind.ipagent.utils.R;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;


@RestController
public class KdlController {

    private static final Logger log = LoggerFactory.getLogger(KdlController.class);


    @Autowired
    private KdlService kdlService;


    @GetMapping("/getDps")
    protected R getDps(HttpServletRequest req, HttpServletResponse resp,int num ) throws Exception {
        //这里每次都拿有个新的ip,有些浪费，所以拿到的ip需要去存储，定时发送请求，直到ip无效在去删除
        String[] dps = kdlService.getDps(num);
        return R.ok().put("ips",dps);
    }

    @GetMapping("/getDpsIpWhitelist")
    protected R getDpsIpWhitelist(HttpServletRequest req, HttpServletResponse resp ) throws Exception {
        String[] ipWhitelist = kdlService.getDpsIpWhitelist();
        return R.ok().put("ips",ipWhitelist);
    }


    @GetMapping("/setDpsIpWhitelist")
    protected R setDpsIpWhitelist(HttpServletRequest req, HttpServletResponse resp,String ip ) throws Exception {
        if(StringUtils.isEmpty(ip)){
            return R.error("ip不能为空");
        }
        String[] dpsIpWhitelist = kdlService.getDpsIpWhitelist();
        if(!containsElement(dpsIpWhitelist,ip)){
            kdlService.setDpsIpWhitelist(addElementToEnd(dpsIpWhitelist,ip));
        }
        return R.ok().put("ips",kdlService.getDpsIpWhitelist());
    }

    @GetMapping("/getDpsValidTime")
    protected R getDpsValidTime(HttpServletRequest req, HttpServletResponse resp,String ipAndPort ) throws Exception {
        //这里每次都拿有个新的ip,有些浪费，所以拿到的ip需要去存储，定时发送请求，直到ip无效在去删除
        if(StringUtils.isEmpty(ipAndPort)){
            return R.error("ip不能为空");
        }
        Map<String, Integer> map = kdlService.getDpsValidTime(ipAndPort);
        return R.ok().put("ips",map);
    }




    @GetMapping("/getKps")
    protected R getKps(HttpServletRequest req, HttpServletResponse resp ) throws Exception {
        //一次性提取全部ip
        String[] dps = kdlService.getKps(9999);
        return R.ok().put("ips",dps);
    }

    @GetMapping("/getKpsIpWhitelist")
    protected R getKpsIpWhitelist(HttpServletRequest req, HttpServletResponse resp ) throws Exception {
        String[] ipWhitelist = kdlService.getKpsIpWhitelist();
        return R.ok().put("ips",ipWhitelist);
    }


    @GetMapping("/setKpsIpWhitelist")
    protected R setKpsIpWhitelist(HttpServletRequest req, HttpServletResponse resp ,String ip) throws Exception {
        if(StringUtils.isEmpty(ip)){
            return R.error("ip不能为空");
        }
        String[] dpsIpWhitelist = kdlService.getKpsIpWhitelist();
        if(!containsElement(dpsIpWhitelist,ip)){
            kdlService.setKpsIpWhitelist(addElementToEnd(dpsIpWhitelist,ip));
        }
        return R.ok().put("ips",kdlService.getKpsIpWhitelist());
    }

    public boolean containsElement(String[] array, String target) {
        for (String element : array) {
            if (element.equals(target)) {
                return true; // 如果找到目标元素，则返回true
            }
        }
        return false; // 如果未找到目标元素，则返回false
    }

    public String[] addElementToEnd(String[] array, String element) {
        String[] newArray = new String[array.length + 1];
        System.arraycopy(array, 0, newArray, 0, array.length);
        newArray[array.length] = element; // 将新元素放置在新数组的最后一个位置
        return newArray;
    }
}
